package es.project.sien7e.main.services;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.validation.Valid;

import org.springframework.stereotype.Service;

import es.project.sien7e.main.PMF;
import es.project.sien7e.main.model.Tipodirectorio;

@Service
public class TipoDirectorioServiceImpl implements TipoDirectorioService {

	@SuppressWarnings("unchecked")
	@Override
	public List<Tipodirectorio> getTiposdirectorios() throws SQLException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Tipodirectorio.class);
		query.setOrdering("id asc");
		return (List<Tipodirectorio>) query.execute();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tipodirectorio> getListaTiposdirectorios() throws SQLException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Tipodirectorio> result = new ArrayList<Tipodirectorio>();
		Tipodirectorio reg1 = new Tipodirectorio();
		reg1.setId((long) 0);
		result.add(reg1);
		Query query = pm.newQuery(Tipodirectorio.class);
		query.setOrdering("id asc");
		result.addAll((List<Tipodirectorio>) query.execute());
		return result;
	}

	@Override
	public Tipodirectorio grabaTipodirectorio(@Valid Tipodirectorio obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Tipodirectorio obj2 = null;
		try {
			obj2 = pm.makePersistent(obj);
		} finally {
			pm.close();
		}
		return obj2;
	}

	@Override
	public Tipodirectorio insertaTipodirectorio(@Valid Tipodirectorio obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Tipodirectorio obj2 = null;
		try {
			obj2 = pm.makePersistent(obj);
		} finally {
			pm.close();
		}
		return obj2;
	}

	@Override
	public Integer borrarTipodirectorio(@Valid Tipodirectorio obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Integer val = 0;
		try {
			pm.deletePersistent(obj);
			val = 1;
		} finally {
			pm.close();
		}
		return val;
	}
}
